import os,datetime
from jili.core import save,load
from jili.tool import get_createtimekey
from jili.data.db import tradecal
def load_localdata(name,hold_days=None,istradeday=False):
    base_url = os.path.join(os.path.dirname(__file__), "localdata")
    url=os.path.join(base_url,name+".pkl")
    if os.path.exists(url):
        if hold_days:
            t=get_createtimekey(url)
            now=datetime.datetime.now()
            if istradeday:
                if now.hour>=18:
                    now=now+datetime.timedelta(days=1)
                pred=tradecal.get_diff_tradeday(now,diff=-hold_days)
                pred=pred+datetime.timedelta(hours=15)
                if t>=pred:
                    return load(url)
                else:
                    return None
            else:
                d=now-t
                d=d.days
                if d >hold_days:
                    return None
                else:
                    return load(url)
        else:
            return load(url)
    else:
        return None
def save_localdata(name,data):
    base_url = os.path.join(os.path.dirname(__file__), "localdata")
    if not os.path.exists(base_url):
        os.mkdir(base_url)
    url = os.path.join(base_url, name)
    save(data,url)